package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author bairimengchang
 * @version 1.0
 * @project sky-take-out
 * @description Redis配置类，用以创建RedisTemplate对象
 * @date 2024/08/09 23:29:18
 */
@Configuration
@Slf4j
public class RedisConfiguration {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * 自定义RedisTemplate，之前dlyk项目是在启动类的main中自定义配置的redis序列化
     * @return 自定义的RedisTemplate对象
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        log.info("开始创建RedisTemplate对象");
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //设置rides连接工厂对象
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置序列化器
        //估计是依赖版本的问题以及redis版本的问题，这里没办法设置value的序列化器，会报错
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

}
